package com.dkd.callback;

import cn.elegent.pay.CallBackService;
import cn.elegent.pay.dto.WatchDTO;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dkd.config.constant.OrderStatus;
import com.dkd.config.constant.PayStatus;
import com.dkd.entity.OrderEntity;
import com.dkd.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 支付回调类
 */
@Service
@Slf4j
public class CallBackServiceImpl implements CallBackService {

    @Autowired
    private OrderService orderService;

    /**
     * 订单成功支付的逻辑
     *
     * @param orderSn
     */
    @Override
    public void successPay(String orderSn) {
        log.info("支付成功回调{}", orderSn);
        var orderEntity = orderService.getByOrderNo(orderSn);  //查询订单
        if (orderEntity != null) {
            orderEntity.setStatus(OrderStatus.ORDER_STATUS_PAYED);//支付成功
            orderEntity.setPayStatus(PayStatus.PAY_STATUS_PAYED);//支付成功
            orderService.updateById(orderEntity);//保存
            // 发送消息通知售货机服务出货
            orderService.vendout(orderEntity.getOrderNo(),orderEntity.getSkuId(),orderEntity.getInnerCode(),orderEntity.getAmount());
        }
    }

    /**
     * 支付失败日志记录
     *
     * @param orderSn
     */
    @Override
    public void failPay(String orderSn) {
        log.info("支付失败回调！" + orderSn);
    }

    /**
     * 需要查询订单状态为已支付返回true
     *
     * @param orderSn
     * @return
     */
    @Override
    public boolean isSuccessPay(String orderSn) {
        var orderEntity = orderService.getByOrderNo(orderSn);
        if (orderEntity != null && orderEntity.getPayStatus().equals(OrderStatus.ORDER_STATUS_PAYED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改订单状态为退款完成
     *
     * @param orderSn
     */
    @Override
    public void successRefund(String orderSn) {
        log.info("退款成功回调！" + orderSn);
        var orderEntity = orderService.getByOrderNo(orderSn);  //查询订单
        if (orderEntity != null) {
            orderEntity.setPayStatus(PayStatus.PAY_STATUS_REFUNDIED);//退款成功
            orderService.updateById(orderEntity);//保存
        }
    }

    /**
     * 退款失败打日志记录--修改状态为已支付
     *
     * @param orderSn
     */
    @Override
    public void failRefund(String orderSn) {
        log.info("退款失败回调！");
        var orderEntity = orderService.getByOrderNo(orderSn);  //查询订单
        if (orderEntity != null) {
            orderEntity.setPayStatus(PayStatus.PAY_STATUS_PAYED);//支付成功
            orderService.updateById(orderEntity);//保存
        }
    }

    /**
     * 查询订单状态是否为退款完成
     *
     * @param orderSn
     * @return
     */
    @Override
    public boolean isSuccessRefund(String orderSn) {
        var orderEntity = orderService.getByOrderNo(orderSn);  //查询订单
        if (orderEntity != null && orderEntity.getPayStatus().equals(PayStatus.PAY_STATUS_REFUNDIED)) {
            return true;
        }
        return false;
    }

    /**
     * 查询所有未支付订单
     *
     * @return
     */
    @Override
    public List<WatchDTO> watchPayOrderSnList() {
        var result = new ArrayList<WatchDTO>();
        var qw = new LambdaQueryWrapper<OrderEntity>();
        //订单状态为未支付
        qw.eq(OrderEntity::getStatus, OrderStatus.ORDER_STATUS_CREATE).gt(OrderEntity::getCreateTime, DateUtil.offsetMinute(new Date(),-10));
        var list = orderService.list(qw);
        for (OrderEntity order : list) {
            var watchDTO = new WatchDTO();
            watchDTO.setOrderSn(order.getOrderNo());//订单编号
            watchDTO.setPlatform(order.getPayType());//平台
            result.add(watchDTO);
        }
        return result;
    }
}